<template>

  <van-row  justify="center" style="margin-top:30px; margin-bottom: 30px;">
    <van-image
        round
        width="180"
        height="180"
        :src=cuteImage
        style="background-position:center"
    />
  </van-row>

  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="账号"
          placeholder="请输入账号"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="userPassword"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 40px">
      <van-button style="margin-top: 10px; background-color: #8BE8B9; width: 200px; margin-left: 45px" round block native-type="submit">
        登 录
      </van-button>
      <van-button style="margin-top: 10px; background-color: #3D94E5; width: 200px; margin-left: 45px" round block native-type="submit"
                  to="/user/register">
        注 册
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import myAxios from "../plugins/myAxios";
import {Toast} from "vant";
import cuteImage from "../assets/images/cute.jpg";

const router = useRouter();
const route = useRoute();

const userAccount = ref('');
const userPassword = ref('');

const onSubmit = async () => {
  const res = await myAxios.post('/user/login', {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
  })
  console.log(res, '用户登录');
  if (res.code === 0 && res.data) {
    Toast.success('登录成功');
    // 跳转到之前的页面
    const redirectUrl = route.query?.redirect as string ?? '/';
    window.location.href = redirectUrl;
  } else {
    Toast.fail('登录失败');
  }
};

</script>

<style scoped>

</style>
